package com.dongdongshop.service.imp;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.page.PageBeen;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.vo.SpecVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbSpecificationServiceImp implements TbSpecificationService {
    @Resource
    private TbSpecificationMapper tbSpecificationMapper;
    @Resource
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;
    @Override
    public PageBeen<TbSpecification> getSpecification(String specName,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbSpecification> tbSpecificationsList= tbSpecificationMapper.selectBygetSpecification(specName);
        PageInfo<TbSpecification> pageInfo = new PageInfo(tbSpecificationsList);
        PageBeen<TbSpecification> pageBeen = new PageBeen<>(pageNum, pageSize, pageInfo.getTotal(), pageInfo.getList(),pageInfo.getPages());
        return pageBeen;

    }

    @Override
    @Transactional
    public int addInfo(TbSpecification tbSpecification,List<TbSpecificationOption> tbsp) {
        int i = tbSpecificationMapper.addInfo(tbSpecification);
        TbSpecificationOption ts = new TbSpecificationOption();
        for (TbSpecificationOption tbs: tbsp) {
            ts.setSpecId(tbSpecification.getId());
            ts.setOptionName(tbs.getOptionName());
            ts.setOrders(tbs.getOrders());
            int i1=tbSpecificationOptionMapper.insertSelective(ts);
        }

        return 1;
    }

    @Override
    public int deleteInfo(Integer[] ids) {
        return tbSpecificationMapper.deleteInfo(ids);
    }

    @Override
    public SpecVO toUpdate(Long id) {
        TbSpecification tb = tbSpecificationMapper.selectByPrimaryKey(id);
        TbSpecificationOptionExample spec = new TbSpecificationOptionExample();
        spec.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbs = tbSpecificationOptionMapper.selectByExample(spec);
        SpecVO specVO = new SpecVO(tb,tbs);
        return specVO;
    }

    @Override
    @Transactional
    public int updateInfo(TbSpecification tbSpecification, List<TbSpecificationOption> tbsp) {
        int i = tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        TbSpecificationOptionExample spec = new TbSpecificationOptionExample();
        spec.createCriteria().andSpecIdEqualTo(tbSpecification.getId());
        int i11 = tbSpecificationOptionMapper.deleteByExample(spec);
        TbSpecificationOption ts = new TbSpecificationOption();
        for (TbSpecificationOption tbs: tbsp) {
            ts.setSpecId(tbSpecification.getId());
            ts.setOptionName(tbs.getOptionName());
            ts.setOrders(tbs.getOrders());
            int i1=tbSpecificationOptionMapper.insertSelective(ts);
        }
        return 1;
    }

}
